package com.hspedu.socket.TCPFileUpload;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author DL5O
 * @version 1.0
 */
public class TCPFileUploadClient {
    public static void main(String[] args) throws Exception {

        //客户连接客户端 8888
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
        String filePath = "D:\\bg.png";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
        byte[] bytes = StreamUtils.streamToByteArray(bis);//输入流中文件转换为字节

        //写入数据到数据通道中
        /*
        方式一：
        OutputStream os = socket.getOutputStream();
        os.write(bytes);
        os.flush();*/
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        bos.write(bytes);
        bos.flush();
        bis.close();//在调工具类的时候已经关了，这里可以不用写
        socket.shutdownOutput();//设置写入数据的结束标记
        System.out.println("写入成功！");

        //读取从客服端发来的消息
        InputStream inputStream = socket.getInputStream();
        String s = StreamUtils.streamToString(inputStream);
        System.out.print(s);
        inputStream.close();
        socket.close();
        System.out.println("退出客服端...");
    }
}
